//
//  KZTimerMomentView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/27.
//  Copyright © 2018 zhukai. All rights reserved.
//

#import "KZTimerMomentView.h"

@implementation KZTimerMomentView
{
    UILabel *_minuteHundredsLabel;      // 分，百位数
    UILabel *_minuteDecadeLabel;        // 分，十位数
    UILabel *_minuteUnitLabel;          // 分，个位数
    UILabel *_colonLabel;               // 冒号
    UILabel *_secondDecadeLabel;        // 秒，十位数
    UILabel *_secondUnitLabel;          // 秒，个位数
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.userInteractionEnabled = false;
        [self setupUI];
    }
    return self;
}

#pragma mark - 创建视图
- (void)setupUI {
    
    CGFloat cx = self.width / 2;
    CGFloat cy = self.height / 2;
    
    _colonLabel = [self label];
    _colonLabel.text = @":";
    [_colonLabel sizeToFit];
    _colonLabel.centerY = cy;
    _colonLabel.centerX = cx;
    
    _minuteUnitLabel = [self label];
    _minuteUnitLabel.centerY = cy;
    _minuteUnitLabel.right = _colonLabel.left;
    _minuteUnitLabel.text = @"0";
    
    _minuteDecadeLabel = [self label];
    _minuteDecadeLabel.centerY = cy;
    _minuteDecadeLabel.right = _minuteUnitLabel.left;
    _minuteDecadeLabel.text = @"0";
    
    _minuteHundredsLabel = [self label];
    _minuteHundredsLabel.centerY = cy;
    _minuteHundredsLabel.right = _minuteDecadeLabel.left;
    _minuteHundredsLabel.text = @"0";
    _minuteHundredsLabel.hidden = true;
    
    _secondDecadeLabel = [self label];
    _secondDecadeLabel.centerY = cy;
    _secondDecadeLabel.left = _colonLabel.right;
    _secondDecadeLabel.text = @"0";
    
    _secondUnitLabel = [self label];
    _secondUnitLabel.centerY = cy;
    _secondUnitLabel.left = _secondDecadeLabel.right;
    _secondUnitLabel.text = @"0";
}
- (UILabel *)label {
    
    UILabel *label = [[UILabel alloc] init];
    label.adjustsFontSizeToFitWidth = true;
    label.text = @"9";
    label.font = FONT(14);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [self addSubview:label];
    [label sizeToFit];
    return label;
}

#pragma mark - setter
- (void)setSecond:(NSUInteger)second {
    
    _second = second;

    if (_second >= 100 * 60) {
        _minuteHundredsLabel.hidden = false;
    }
    
    NSString *text = [NSString stringWithFormat:@"%03i:%02i", (int)_second/60, (int)_second%60];
    _minuteHundredsLabel.text = [text substringWithRange:NSMakeRange(0, 1)];
    _minuteDecadeLabel.text = [text substringWithRange:NSMakeRange(1, 1)];
    _minuteUnitLabel.text = [text substringWithRange:NSMakeRange(2, 1)];
    _colonLabel.text = [text substringWithRange:NSMakeRange(3, 1)];
    _secondDecadeLabel.text = [text substringWithRange:NSMakeRange(4, 1)];
    _secondUnitLabel.text = [text substringWithRange:NSMakeRange(5, 1)];
}


@end
